<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: HEAD, GET, POST, PUT, DELETE');
header('Access-Control-Max-Age: 3628800');
function httpgets($url, $headers) {
	$ch = curl_init();
	//设置选项，包括URL
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	$output = curl_exec($ch);
	if ($output === false) {
		$re = curl_error($ch);
		curl_close($ch);
		return $re;
	} else {
		curl_close($ch);
		return $output;
	}
}

$h = array('User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) CometHTTP', 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Host: music.163.com', 'Cookie: _ga=GA1.2.1593287870.1453202858; vjlast=1454585665.1473415877.11; vinfo_n_f_l_n3=5cfc9539ca940d2f.1.14.1454585665081.1471064171838.1473415921469; __utma=94650624.1648760954.1452740844.1468756726.1469237860.197; __utmz=94650624.1468756726.196.5.utmcsr=82cat.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __remember_me=true; MUSIC_U=2417341f993d9a042607da5e7397829befda25ce84c32b90a28ae1ea13463f513ebffc1c244a35006cbb864e4c89670e3c94dba7d22c94a8bf122d59fa1ed6a2; __csrf=e1f403ce66b37fe7f48de7487e846d41; JSESSIONID-WYYY=1ecb2ab012473a04940b2b2e763651e0b6d933ab142ef141caaeb4523ee278fd8bc865bb69abc789212b8e195b4a3c8c9d3964ab9d47b47d75ed1968f5c349ffd003bb89475145e51387745cffea0d4081234525a1d1ca43640b0ce240027eb94d1595e74a611e4eb5da57dc771ef881de537110d800d6cdf3eccc528239bd96639e7cca%3A1474078331074; _iuqxldmzr_=25');

if (isset($_GET['id']) && isset($_GET['type'])) {
	$id = $_GET['id'];
	if ($_GET['type'] == 'dj') {
		$url = "http://music.163.com/api/dj/program/detail/?id=" . $id;
		$re = httpgets($url, $h);

		$re = json_decode($re);
		$musicsrc = $re -> program -> mainSong -> mp3Url;
		$musicimg = $re -> program -> mainSong -> album -> picUrl;

		$re = array('m' => $musicsrc, 'p' => $musicimg);
		echo json_encode($re);
	} else if ($_GET['type'] == 'song') {
		$url = "http://music.163.com/api/song/detail/?id=" . $id . "&ids=[" . $id . "]&csrf_token=";
		$re = httpgets($url, $h);
		$re = json_decode($re);
		$name = $re -> songs[0] -> name;
		$musicsrc = $re -> songs[0] -> mp3Url;
		$musicimg = str_replace('http://', 'https://', $re -> songs[0] -> album -> picUrl);
		$artists = $re -> songs[0] -> artists;
		$artist = "";
		foreach ($artists as $a) {
			$artist .= $artist == "" ? $a -> name : "/" . $a -> name;
		}
		$album = $re -> songs[0] -> album -> name;

		$url = "http://music.163.com/api/song/lyric?id=" . $id . "&os=android&lv=-1&tv=-1";
		$re = httpgets($url, $h);
		$re = array('file' => $musicsrc, 'name' => $name, 'poster' => $musicimg, 'poster' => $musicimg, 'lyric' => $re, 'artist' => $artist, 'album' => $album);
		echo json_encode($re);
	} else {
		$re = array('error' => 'illegal request');
		echo json_encode($re);

	}
} else {
	$re = array('error' => 'illegal request');
	echo json_encode($re);
}
?>